dojo.provide("lorelei.translate");
dojo.require("dojo.io.script");

// Lorelei Google AJAX Language API Wraping script
// Copyright (c)2008 Logue <http://logue.be/> All rights reserved.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

if (!google.language) console.error('google AJAX Language API has not loaded. please insert \'google.load("language", "1");\' before this.');

// 多言語化（domを入力するとその部分の言語を適切な言語に自動翻訳）
lorelei.translate = {
	meta: {
		"@prefix": "<http://purl.org/net/ns/doas#>",
		"@about": "<http://mabinogi.logue.be/js/lolerei/articles.js>", a: ":JavaScript",
		 title: "Lorelei Google AJAX Language API Wraping script",
		 created: "2008-10-29", release: {revision: "1.0.2", created: "2008-10-30"},
		 author: {name: "Logue", homepage: "<http://logue.be/>"},
		 license: "<http://www.gnu.org/licenses/gpl-3.0.html>"
	},
	// 言語設定。
	// サーバー側の言語出力をlorelei.translate.lang = <?php echo $lang ?>のような形で上書き指定すること。
	lang :'en',
	// コールバック関数。
	// 他のスクリプトで、翻訳処理完了時の処理をlorelei.translate.callback = function(response){..}という形で上書き指定する。
	callback : function(response){
		console.log(response);
		return response;
	},
	// CSSセレクタによる翻訳
	// lorelei.translate.translate('div.post p');でdiv class="post"以下のpタグ要素毎に翻訳を実行する。
	translate : function(target){
		var i = 0;
		dojo.query(target).forEach(function(elem){
			var source = elem.innerHTML;
			lorelei.translate.callback = function(response){
				dojo.query(target)[i].innerHTML = response;
				i++;
			}
			lorelei.translate.google_detect(source);
		});
	},
	// 言語判別処理。入力されたテキストがサーバー設定の言語と異なる場合、翻訳処理に移る。
	google_detect: function(source) {
		this.lang = this.lang.slice(0,2);
		dojo.io.script.get({
			url: "http://ajax.googleapis.com/ajax/services/language/detect", 
			content: {v : "1.0", q: source },
			handleAs: "application/json",
			preventCache: true,
			callbackParamName: "callback",
			load: function(response, ioArgs) {
				if (response.responseStatus == 200){
					lorelei.translate.google_translate(source, response.responseData.language);
				}else{
					console.error(response.responseDetails);
					console.dir(this);
				}
				return response;
			},
			error: function(response, ioArgs) {
				console.error("HTTP status code: ", ioArgs.xhr.status);
				return response;
			}
		});
	},
	// 翻訳処理
	google_translate : function(source,translate_to){
		// サーバー側の言語設定が読み込めない場合英語として処理する
		if (lorelei.translate.lang == '') lorelei.translate.lang = 'en';
		// 判別された言語とサーバー側の言語が異なる場合、翻訳を実行する。
		if (translate_to != lorelei.translate.lang){
			dojo.io.script.get({
				url: "http://ajax.googleapis.com/ajax/services/language/translate",
				content: {v : "1.0", q: source, langpair: translate_to + "|" + lorelei.translate.lang },
				handleAs: "application/json",
				preventCache: true,
				callbackParamName: "callback",
				load: function(response, ioArgs) {
					if (response.responseStatus == 200){
						lorelei.translate.callback(response.responseData.translatedText);
					}else{
						console.error(response.responseDetails);
						console.dir(this);
					}
					return response;
				},
				error: function(response, ioArgs) {
					console.error("HTTP status code: ", ioArgs.xhr.status);
					return response;
				}
			});
		}
	}
}

console.log(lorelei.translate.meta.title,lorelei.translate.meta.release,' has been loaded.');